import pandas as pd

def get_seq_list_from_log(filename):
    seq_list = []

    if 'client' in filename:
        with open(filename) as f:
            line = f.readline()
            while line:
                if 'INCOMING' in line:
                    seq = int(line.split(',')[1].split('=')[1])
                    seq_list.append(seq)
                line = f.readline()
    elif 'server' in filename:
        with open(filename) as f:
            line = f.readline()
            while line:
                if 'OUTGOING' in line:
                    seq = int(line.split(',')[1].split('=')[1])
                    seq_list.append(seq)
                line = f.readline()       
    
    return seq_list


def get_sat_loss(file_sender, file_receiver):
    # sender_seq_list = get_seq_list_from_log('client-1685618712-1685618712')
    # receiver_seq_list = get_seq_list_from_log('server-1685618669-26865')
    
    # sender_seq_list = get_seq_list_from_log('client-1685623430-1685623430')
    # receiver_seq_list = get_seq_list_from_log('server-1685623413-29598')

    sender_seq_list = get_seq_list_from_log(file_sender)
    receiver_seq_list = get_seq_list_from_log(file_receiver)


    sender_seq_set = set(sender_seq_list)
    receiver_seq_set = set(receiver_seq_list)

    diff = sender_seq_set - receiver_seq_set
    # print(sorted(diff))

    print('client seq list num', len(sender_seq_list))
    print('server seq list num', len(receiver_seq_list))
    print('client seq set num', len(sender_seq_set))
    print('server seq set num', len(receiver_seq_set))
    print('packet loss num', len(diff))

    return diff



if __name__ == "__main__":
    # loss_set = get_sat_loss('client-1685623735-1685623735', 'server-1685623727-29672')
    # loss_set = get_sat_loss('client-1686210879-1686210879', 'server-1686210847-28911')
    loss_set = get_sat_loss('client-1686214831-1686214831', 'server-1686214764-30483')
    loss_set = sorted(loss_set)
    # with open('satloss_tc50M150000_background.csv', 'w') as f:
    #     for seq in loss_set:
    #         f.write(str(seq) + '\n')
